package com.unme.controller;


import com.alibaba.fastjson.JSONObject;
import com.unme.model.LabelRela;
import com.unme.service.LabelRelaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/aph")
public class LabelRelaController {
    @Autowired
    private LabelRelaService labelRelaService;

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/addLabelRela", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> addLabelRela(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        LabelRela labelRela=new LabelRela();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{
            labelRela.setuId(jsonParam.getInteger("uId"));
            labelRela.setLabelId(jsonParam.getInteger("labelId"));
            labelRela.setuType(jsonParam.getInteger("uType"));
            labelRelaService.addLabelRela(labelRela);
            map.put("message","数据添加成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据添加失败！");
            return map;

        }
    }


    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/findLabelRela", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> findLabelRela(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        LabelRela labelRela=new LabelRela();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{
            map.put("data",labelRelaService.findLabelRela(labelRela));
            map.put("message","数据查询成功！");
            return map;
        }catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据查询失败！");
            return map;
        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/deleteLabelRela", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> deleteLabelRela(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        LabelRela labelRela=new LabelRela();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{

           labelRelaService.deleteLabelRela(labelRela);
            map.put("message","数据删除成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据删除失败！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/updateLabelRela", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> updateLabelRela(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        LabelRela labelRela=new LabelRela();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            labelRela.setuId(jsonParam.getInteger("uId"));
            labelRela.setLabelId(jsonParam.getInteger("labelId"));
            labelRela.setuType(jsonParam.getInteger("uType"));
            labelRelaService.updateLabelRela(labelRela);
            map.put("message", "数据更新成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据更新成功！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getLabelById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> getLabelById(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        LabelRela labelRela=new LabelRela();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            labelRela.setuId(jsonParam.getInteger("uId"));
            labelRela.setLabelId(jsonParam.getInteger("labelId"));
            labelRela.setuType(jsonParam.getInteger("uType"));
            labelRelaService.getLabelRelaById(labelRela);
            map.put("message", "数据获取成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据获取失败！");
            return map;

        }
    }
}
